<template>
  <el-row>
    <el-col :span="4" :offset="3">
      <el-card :body-style="{ padding: '0px' }">
        <img
          v-if="book.imgUrl"
          :src="baseUrl + book.imgUrl"
          class="book-image"
        />
        <div class="card-bottom">
          <span>{{ book.bookName }}</span>
          <div class="book-descriptions clearfix">
            <span class="book-publish">{{ book.publisher }}</span>
          </div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="10" :offset="3">
      <table>
        <tbody class="book-info">
          <tr class="book-name">
            <td>bookName</td><td>{{ book.bookName }}</td>
          </tr>
          <tr>
            <td>ISBN</td><td>{{ book.isbn }}</td>
          </tr>
          <tr>
            <td>author</td><td>{{ book.author }}</td>
          </tr>
          <tr>
            <td>publisher</td><td>{{ book.publisher }}</td>
          </tr>
          <tr>
            <td>price</td><td>{{ book.unitPrice }}</td>
          </tr>
          <tr class="book-sumary">
            <td>summary</td><td>{{ book.summary }}</td>
          </tr>
          <tr>
            <td>rackNumber</td><td>{{ setRackNumber(book.rackNumber) || '暂无' }}</td>
          </tr>
          <tr>
            <td>total</td><td>{{ book.allNum }}</td>
          </tr>
          <tr>
            <td>remain</td><td>{{ book.lastNum }}</td>
          </tr>
          <!-- <tr>
            <td>预约量</td><td>{{ book.reserveNum || 0 }}</td>
          </tr> -->
          <tr>
            <!-- <td><el-button
              type="primary"
              @click="borrowBook"
              :disabled="book.lastNum < 1"
              >借阅
            </el-button></td> -->
            <td><el-button
              type="primary"
              @click="reserveBook"
              :disabled="book.lastNum < 1"
              >reserve
            </el-button></td>
          </tr>
        </tbody>
      </table>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import * as bookApi from "@/api/books";
import { BASE_URL } from "@/config/constant";
import { useUserStore } from "@/stores/user";
import { ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router";
import { useBook } from "./business";

const baseUrl = BASE_URL;
const book = useBook();
const router = useRouter();
const route = useRoute();
const store = useUserStore();
const reserveBook = () => {
  if (!store.isLogin) {
    router.push("/login");
    return;
  }
  bookApi.reserveBook({ isbn: route.params.isbn as string }).then(() => {
    ElMessage.success("reserved successfully，please borrow in 4 hours，or the reserve will be invalid！");
    setTimeout(() => {
      router.push({
        path: "/",
      });
    }, 1000);
  })
  .catch(err => {
    ElMessage.error(err);
  });
};
const setRackNumber = (rackNumber) => {
  const floor = Math.floor(rackNumber / 10000);
  const face = Math.floor(rackNumber % 10000 / 100);
  const row = Math.floor(rackNumber % 100 / 10);
  const col = Math.floor(rackNumber % 10);
  return `floor:${floor} face:${face} row:${row} col:${col}`;
}
</script>

<style scoped>
.el-row {
  margin-top: 100px;
}
.card-bottom {
  padding: 14px;
  text-align: left;
}
.book-publish {
  font-size: 13px;
  color: #999;
}

.book-descriptions {
  margin-top: 13px;
  line-height: 12px;
}

.book-image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.book-info {
  list-style: none;
  text-align: left;
}

.book-info td {
  margin: 10px;
  min-height: 30px;
  line-height: 30px;
}
.book-info td:first-child {
  font-weight: bold;
  min-width: 100px;
}
.book-name td {
  font-size: 32px;
  font-weight: bold;
  line-height: 40px;
}
.button {
  display: inline-block;
}
</style>
